package com.longrise.co.ui.home.widget;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.ui.home.adapter.PopWindowAdapter;
import com.longrise.co.ui.home.model.DictionaryBean;
import com.longrise.co.utils.Constants;
import com.ly.common.baserx.RxBus;
import com.ly.common.popwindow.BasePopupWindow;

import java.util.List;

import static com.longrise.co.R.id.recyclerView;

/**
 * Created by luoyang on 2017-7-13.
 */

public class CompanyQueryPopWindow extends BasePopupWindow implements BaseQuickAdapter.OnRecyclerViewItemClickListener {

    private RecyclerView mRecyclerView;
    private PopWindowAdapter mAdapter;


    public CompanyQueryPopWindow(Activity context) {
        super(context, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initPopView();
    }

    private void initPopView() {
        mRecyclerView = (RecyclerView)mPopupView.findViewById(recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new PopWindowAdapter(null);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void setNewData(List<DictionaryBean> listData) {
        mAdapter.setNewData(listData);
    }

    @Override
    public View getPopupView() {
        return getPopupViewById(R.layout.popwindow_company_query);
    }

    /**
     * 设置动画效果
     * @return
     */
    @Override
    public View getAnimationView() {
        return mPopupView.findViewById(R.id.pop_content);
    }


    @Override
    public void showPopupWindow(View v) {
        try {
            mPopupWindow.showAsDropDown(v);
            if (getShowAnimation() != null && mAnimaView != null) {
                mAnimaView.clearAnimation();
                mAnimaView.startAnimation(getShowAnimation());
            }
            if (getShowAnimation() == null && getShowAnimator() != null && mAnimaView != null) {
                getShowAnimator().start();
            }
        } catch (Exception e) {
            Log.w("error","error");
        }
    }

    @Override
    protected Animation getShowAnimation() {
        AnimationSet set=new AnimationSet(true);
        set.setInterpolator(new DecelerateInterpolator());
        set.addAnimation(getScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0));
        set.addAnimation(getDefaultAlphaAnimation());
        return set;
    }

    /**
     * 点击取消view
     * @return
     */
    @Override
    protected View getClickToDismissView() {
        return null;
    }

    @Override
    public void onItemClick(View view, int i) {
        RxBus.getInstance().post(Constants.RX_EVENT_COMPANY_SEARCH, mAdapter.getItem(i));
    }
}
